package 字符串的扩展类;

import java.util.StringJoiner;

/**
 * @author chihb
 * @create-time 2024/6/29 16:02
 * @description
 */
public class Test {
    public static void main(String[] args) {
        // StringBuilder 是可变字符串容器，String是不可变容器，StringBuilder在拼接，修改的时候效率非常高
        // StringBuilder是线程不安全的，StringBuffer是线程安全的，StringBuilder和StringBuffer在用法上是一模一样的
        String s="";
        StringBuilder s1=new StringBuilder();
        s1.append("55875665");
        System.out.println(s1);
        s1.reverse();
        System.out.println(s1);
        //把StringBuilder对象有转换成String类型
        String rs=s1.toString();

        System.out.println("====================");
        System.out.println(getData(new int[]{11, 22, 33}));

        //StringJoiner提高字符串操作效率，可以使代码更简洁
        StringJoiner s4=new StringJoiner(",","[","]");//间隔符 开始 结束
        s4.add("java1");
        s4.add("java2");
        s4.add("java3");
        s4.add("java4");
        String s5=s4.toString();
        System.out.println(s5);

        String[] arr =s5.replace("[","").replace("]","").split(",");
        for(String i:arr){
            System.out.println(i);
        }
    }

    public static String getData(int[] arr){
        if(arr==null) return null;
        StringBuilder s=new StringBuilder();
        s.append('[');
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                s.append(i);
            }else {
                s.append(i).append(',');
            }
        }
        s.append(']');
        return s.toString();
    }
}
